package com.yanqu.road.server.manger.activity.guagifts;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.GuaGiftsProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.guagifts.GuaGiftsModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.executor.GeneralTask;

public class GuaGiftsReceiveGiftTask extends GeneralTask {
    private long sendSeverId;
    private long sendUserId;
    private long targetId;
    private int giftId;
    private int giftCount;
    private String content;

    public GuaGiftsReceiveGiftTask(long sendServerId, long sendUserId, long targetId, int giftId, int giftCount, String content) {
        this.sendSeverId = sendServerId;
        this.sendUserId = sendUserId;
        this.targetId = targetId;
        this.giftId = giftId;
        this.giftCount = giftCount;
        this.content = content;
    }

    @Override
    public void doRun() {
        GamePlayer player = GamePlayerMgr.getPlayer(targetId);
        if (null != player) {
            GuaGiftsProto.CrossGuaGiftsSendGiftRespMsg.Builder resultMsg = player.getModule(GuaGiftsModule.class).receiveGift(sendUserId, giftId, giftCount, content);

            GuaGiftsProto.CrossGuaGiftsSendGiftRespMsg.Builder backMsg = GuaGiftsProto.CrossGuaGiftsSendGiftRespMsg.newBuilder();
            backMsg.setRet(resultMsg.getRet());
            backMsg.setRemainCanReceive(resultMsg.getRemainCanReceive());
            backMsg.setGiftId(giftId);
            backMsg.setGiftCount(giftCount);
            backMsg.setTargetUserId(targetId);
            backMsg.setSendUserId(sendUserId);
            if (resultMsg.getRet() == 0) {
                backMsg.setTargetUserId(targetId);
                backMsg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
                backMsg.setGiftId(giftId);
                backMsg.setGiftCount(giftCount);
                backMsg.setContent(content);
                backMsg.setSendUserId(sendUserId);
            }

            if (sendSeverId == GameServer.getInstance().getServerId()) {
                GamePlayer backPlayer = GamePlayerMgr.getPlayer(sendUserId);
                if (backPlayer != null) {
                    backPlayer.getModule(GuaGiftsModule.class).sendGiftSuccessBack(backMsg.build());
                }
            } else {
                GamePlayerMgr.sendToGameServer(sendSeverId, GameProtocol.S_GUAGIFTS_SEND_GIFTS_ASYNC_BACK, player.getUserId(), backMsg, null);
            }
        } else {
            GuaGiftsProto.CrossGuaGiftsSendGiftRespMsg.Builder backMsg = GuaGiftsProto.CrossGuaGiftsSendGiftRespMsg.newBuilder();
            backMsg.setRet(GameErrorCode.E_USER_NO_FOUND);
            backMsg.setGiftId(giftId);
            backMsg.setGiftCount(giftCount);
            backMsg.setTargetUserId(targetId);
            backMsg.setSendUserId(sendUserId);
            GamePlayerMgr.sendToGameServer(sendSeverId, GameProtocol.S_GUAGIFTS_SEND_GIFTS_ASYNC_BACK, targetId, backMsg, null);
        }
    }
}
